<?php


namespace App\Logics\ArtHtml;


class AdminForm
{

    protected $module;
    protected $controller;

    // 表单页面
    public function __construct($module, $controller)
    {
        $this->module     = $module;
        $this->controller = $controller;
    }

    // 输入框
    public function formHidden($field, $val)
    {
        $html = ' value="'.$val.'"   name="'.$field.'"';

        return '<input type="hidden" '.$html.'/>';
    }

    // 输入框
    public function formShow($val)
    {
        // <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
        return '<div class="layui-form-mid layui-word-aux">'.$val.'</div>';
    }

    // 输入框
    public function formInput($field, $val, $label = '', $width = '336px')
    {
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请填写'.$label.'"  ';
        }

        return '<input type="text" class="layui-input" autocomplete="off" '.$html.'/>';
    }

    // 日期时间
    public function formDateTime($field, $val, $label = '', $type = 'date')
    {

        if($type == 'datetime'){
            $width = '180px';
        } elseif($type == 'date'){
            $width = '120px';
        } elseif($type == 'time'){
            $width = '120px';
        }


        $html = ' value="'.$val.'"   name="'.$field.'"   id="'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请填写'.$label.'"  ';
        }

        // js
        $js = "<script type='text/javascript'>
        laydate.render({
            elem: '#{$field}'
            ,type: '{$type}'
          });
	    </script>";

        return '<input type="text" class="layui-input" autocomplete="off" '.$html.'/>'.$js;
    }

    // 输入框：数字
    public function formNumber($field, $val, $width = '136px')
    {
        // <input type="number" min="1" onkeyup="this.value=this.value.replace(/\D/, '');" class="laypage_skip">
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';

        return '<input type="number" min="0" class="layui-input" autocomplete="off" onkeyup="this.value=this.value.replace(/\D/, \'\');" '.$html.'/>';
    }

    // 输入框
    public function formTextarea($field, $val, $width = '336px')
    {

        return '<textarea name="'.$field.'" id="'.$field.'" class="layui-textarea" >'.$val.'</textarea>';
    }

    // 输入框
    public function _formUeditor($field, $val)
    {
        $url    = url('/innerend/deal/ueditor');
        $script = " var	ue = UE.getEditor('".$field."',{serverUrl:\"".$url."\",});";  // ,{serverUrl:\"".$url."\",}

        return '<script name="'.$field.'" id="'.$field.'" type="text/plain">'.$val.'</script><script>'.$script.'</script>';
    }

    // 密码框
    public function formPassword($field, $val, $label = '', $width = '336px')
    {
        $html = ' value="'.$val.'"   name="'.$field.'" style="width:'.$width.';"';
        if($label){
            $html .= '  placeholder="请输入'.$label.'"  ';
        }

        return '<input type="password" class="layui-input" autocomplete="off"  '.$html.'/>';
    }


    // 单选框
    public function _formRadio($field, $val, $data)
    {
        // <input type="radio" name="sType"  value="记者" checked="checked" id="sType1" /><label for="sType1">媒体</label>
        $all  = '';
        $html = ' type="radio"  name="'.$field.'" ';
        foreach($data as $key => $show){
            $check = '';
            if($key == $val){
                $check = ' checked="checked" ';
            }

            $_id    = $field.'_'.$key;
            $_label = '<label for="'.$_id.'">'.$show.'</label>';
            $radio  = '<input '.$html.$check.' value="'.$key.'" id="'.$_id.'" />';
            $all    .= $radio.$_label.'&nbsp;&nbsp;';
        }

        return $all;
    }

    // 单选框
    public function formCheckbox($field, $vals, $data)
    {
        // <input type="checkbox" value="{{$_row['id']}}" name="id"/>财经&nbsp;
        $all  = '';
        $html = ' type="checkbox"  name="'.$field.'[]" ';
        foreach($data as $key => $show){
            $check = '';
            if(in_array($key, $vals)){
                $check = ' checked="checked" ';
            }

            $_id    = $field.'_'.$key;
            $_label = '<label for="'.$_id.'">'.$show.'</label>';
            $radio  = '<input '.$html.$check.' value="'.$key.'" id="'.$_id.'" '.$_label;
            $all    .= '<div style="display: flow;">'.$radio.'</div>';
        }

        return $all;
    }

    // 下拉框（一维数组）
    public function formSelect($field, $val, $data)
    {
        $all  = '<option value="" >请选择...</option>';
        $html = '<select class="layui-input" name="'.$field.'"  id="'.$field.'" style="width: 120px;display: none;" > ';
        foreach($data as $key => $show){
            $selected = '';
            if($key == $val){
                $selected = ' selected="selected" ';
            }
            $option = '<option value="'.$key.'" '.$selected.'>'.$show.'</option>';
            $all    .= $option;
        }

        return $html.$all.'</select>';
    }

    // 下拉框（二维数组）
    public function formSelectLists($field, $val, $lists, $val_field, $show_field)
    {
        $all  = '<option value="" >请选择...</option>';
        $html = ' <select class="layui-input" name="'.$field.'"  id="'.$field.'" style="width: 120px; display: inline;"> ';
        foreach($lists as $key => $item){
            $selected = '';
            $_level   = '';
            if($val == $item[$val_field]){
                $selected = ' selected="selected" ';
            }
            if(!empty($item['level'])){
                $_level = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $item['level']).'|--';
            }
            $_show  = $_level.$item[$show_field];
            $_key   = $item[$val_field];
            $option = '<option value="'.$_key.'" '.$selected.'>'.$_show.'</option>';
            $all    .= $option;
        }

        return $html.$all.'</select>';
    }

    #########################################################################################################
    // ajax搜索（被：ajaxSingleFromList 替换）（待删除）
    public function _ajaxSearch($field, $val, $ajaxFun)
    {
        $fun  = ucfirst($ajaxFun);
        $html = '<input type="text" class="layui-input" value="'.$val.'" name="show'.$field.'" id="show'.$field.'"  style="width:145px;" onkeyup="searchUser(this,"'.$field.'");" autocomplete="off"/>';
        $html = '<input type="text" class="layui-input" value=""  style="width:145px;" onkeyup="search'.$fun.'(this,\''.$field.'\');" autocomplete="off"/>';

        $html .= '<span id="show_'.$field.'_ID">'.$field.':'.$val.'</span>';
        $html .= '<input type="hidden" value="'.$val.'" name="'.$field.'" id="hide_'.$field.'_ID"/>';
        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';

        return $html;
    }

    // ajax搜索（适应列表搜索的第三版）（未测试，可以删除）
    public function _ajaxSearch2($field, $val, $ajaxAction, $valueFiled = 'id', $showFiled = 'username')
    {
        $html = '<input type="text" class="layui-input" value="'.$val.'" name="show'.$field.'" id="show'.$field.'"  style="width:145px;" onkeyup="searchUser(this,"'.$field.'", "'.$showFiled.'");" autocomplete="off"/>';

        $url  = url('/'.$this->module.'/selector/'.$ajaxAction);
        $html = '<input type="text" class="layui-input" value=""  style="width:145px;" onkeyup="searchList(\''.$url.'\', this,\''.$field.'\',\''.$valueFiled.'\',\''.$showFiled.'\');" autocomplete="off"/>';
        $html .= '<span id="show_'.$field.'_ID">'.$field.':'.$val.'</span>';
        $html .= '<input type="hidden" value="'.$val.'" name="'.$field.'" id="hide_'.$field.'_ID"/>';
        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';

        return $html;
    }

    // ajax搜索（选定多个）（被：ajaxMultiFormList 替换）（待删除）
    public function _ajaxSearchList($field, $valLists, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        $fun  = ucfirst($ajaxFun);
        $html = '<input type="text" class="layui-input" value=""  style="width:145px;" onkeyup="search'.$fun.'(this, \''.$field.'\');" autocomplete="off"/>';
        $html .= '<span id="show_'.$field.'_list">';
        foreach($valLists as $_val){
            $html .= '<span class="spanList" data-id="'.$_val[$show_id].'">'.$_val[$show_label].'&nbsp;<a href="javascript:;" onclick="$(this).parent().remove()"> 删</a></span>&nbsp;&nbsp;';
        }
        $html .= '</span>';

        //$html .= '<input type="hidden" value="'.$val.'" name="hide'.$fun.'ID" id="hide'.$fun.'ID"/>';
        $html .= '<input type="hidden" value="" name="'.$field.'" id="'.$field.'" />';

        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';
        $html .= '<script>var id_'.$field.'_array=[];
            $("#search_'.$field.'_List").parent().find(".spanList").each(function(){
                //id_'.$field.'_array.push({id:$(this).attr(\'data-id\') ,name:$(this).attr(\'data-name\')});
                id_'.$field.'_array.push($(this).attr(\'data-id\'));
            })
            console.log(\''.$field.'\',id_'.$field.'_array)
            $("#'.$field.'").val(JSON.stringify(id_'.$field.'_array));</script>';

        return $html;
    }

    // ajax搜索（选定多个）（可用）
    public function _ajaxMultiFormList($field, $valLists, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        $showHtml = '<span id="show_'.$field.'_list">';
        foreach($valLists as $_val){
            $showHtml .= '<span class="spanList" data-id="'.$_val[$show_id].'">'.$_val[$show_label].'&nbsp;<a href="javascript:;" onclick="$(this).parent().remove()"> 删</a></span>&nbsp;&nbsp;';
        }
        $showHtml .= '</span>';

        $html = $this->ajaxSearchHtml($showHtml, $field, $ajaxFun, $show_id, $show_label);

        return $html;
    }

    // ajax搜索（选定单个）（可用）
    public function _ajaxSingleFromList($field, $_valInfo, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        /*$showHtml = '';
        if($_valInfo){
            $showHtml = '<span class="spanList" data-id="'.$_valInfo[$show_id].'">'.$_valInfo[$show_label].'('.$show_id.':'.$_valInfo[$show_id].')&nbsp;<a href="javascript:;" onclick="$(this).parent().remove()"> 删</a></span>&nbsp;&nbsp;';
        }*/

        if($_valInfo){
            $showLabel = $_valInfo[$show_label] ?? '';
            $showVal   = $_valInfo[$show_id] ?? '';
            $showHtml  = '<span id="show_'.$field.'_ID">'.$showLabel.':('.$show_id.':'.$showVal.')</span>';
            $showHtml  .= '<input type="hidden" value="'.$showLabel.'" name="'.$field.'" id="hide_'.$field.'_ID"/>';
        } else{
            $showHtml = '<span id="show_'.$field.'_ID"></span>';
            $showHtml .= '<input type="hidden" value="" name="'.$field.'" id="hide_'.$field.'_ID"/>';
        }
        $html = $this->ajaxSearchHtml($showHtml, $field, $ajaxFun, $show_id, $show_label);

        return $html;
    }

    // ajax搜索(老版本)
    public function _ajaxSearchList2($field, $valLists, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        $fun  = ucfirst($ajaxFun);
        $html = '<input type="text" class="layui-input" value=""  style="width:145px;" onkeyup="searchUser(this, \''.$field.'List\');" autocomplete="off"/>';
        foreach($valLists as $_val){
            $html .= '<span class="spanList" data-id="'.$_val[$show_id].'">'.$_val[$show_label].'&nbsp;<a href="javascript:;" onclick="$(this).parent().remove()"> 删</a></span>&nbsp;&nbsp;';
        }

        //$html .= '<input type="hidden" value="'.$val.'" name="hide'.$fun.'ID" id="hide'.$fun.'ID"/>';
        $html .= '<input type="hidden" value="" name="'.$field.'" id="'.$field.'" />';

        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="'.$field.'List"></div>';
        $html .= '<script>var id'.$field.'_array=[];
            $("#'.$field.'List").parent().find(".spanList").each(function(){
                //id'.$field.'_array.push({id:$(this).attr(\'data-id\') ,name:$(this).attr(\'data-name\')});
                id'.$field.'_array.push($(this).attr(\'data-id\'));
            })
            console.log(id'.$field.'_array)
            $("#'.$field.'").val(JSON.stringify(id'.$field.'_array));</script>';

        return $html;
    }

    // Ajax搜索的html
    protected function _ajaxSearchHtml($showHtml, $field, $ajaxFun, $show_id = 'id', $show_label = 'username')
    {
        // 展示输入框：带Ajax操作
        $fun  = strtolower($ajaxFun);
        $url  = url('/'.$this->module.'/selector/'.$fun);
        $html = '<input type="text" class="layui-input" value=""  style="width:145px;" onkeyup="ajaxSearch(\''.$url.'\', this, \''.$field.'\', \''.$show_id.'\', \''.$show_label.'\');" autocomplete="off"/>';


        // 展示显示的值
        /*$html .= '<span id="show_'.$field.'_list">';
        $html .= $showHtml;
        $html .= '</span>';*/
        $html .= $showHtml;

        // 隐藏域 搜索后的下拉 与 下拉后的选择
        $html .= '<input type="hidden" value="" name="'.$field.'" id="'.$field.'" />';
        $html .= '<div style="display:none;position: absolute;top: 30px;z-index: 1000;border: 1px solid #c3c3c3;width:143px;background-color: #fff;" id="search_'.$field.'_List"></div>';


        /*$html .= '<script>var id_'.$field.'_array=[];
            $("#search_'.$field.'_List").parent().find(".spanList").each(function(){
                //id_'.$field.'_array.push({id:$(this).attr(\'data-id\') ,name:$(this).attr(\'data-name\')});
                id_'.$field.'_array.push($(this).attr(\'data-id\'));
            })
            console.log(\''.$field.'\',id_'.$field.'_array)
            $("#'.$field.'").val(JSON.stringify(id_'.$field.'_array));</script>';*/

        return $html;
    }

    #########################################################################################################

    // 构建图片
    public function _formImage($field, $val)
    {
        $isDel = '';
        $show  = $val;
        if(empty($show)){
            $show  = "/static/upfile.png";
            $isDel = "style=\"display: none;\"";
        }
        $html = '<img id="'.$field.'" src="'.$show.'" style="z-index: 1;" height="128">';
        $html .= '<input type="hidden" style="width:300px" value="'.$val.'" id="'.$field.'_image" name="'.$field.'">';
        $html .= '<button class="btn btn-primary radius" type="button" id="delThumb" '.$isDel.'>删除图片</button>';
        $html .= '<script>get_file_fun("'.$field.'");$("#delThumb").on("click",function(){$("#'.$field.'").attr("src","/static/upfile.png");$("#'.$field.'_image").val("");$("#delThumb").hide();});</script>';

        return $html;
    }

    // 构建图片
    public function _formShowImage($val)
    {
        $show = $val;
        if(empty($show)){
            $show = "/static/upfile.png";
        }
        $html = '<a href="'.$val.'" target="_blank"><img id="thumb" src="'.$show.'" style="z-index: 1;" height="128" /></a>';

        return $html;
    }


    // 构建以后
    public function makeItem($label, $formHtml, $msg = '')
    {
        $msgHtml   = $msg ? '&nbsp;<span style="color: darkgray;">（'.$msg.'）</span>' : '';
        $labelHtml = '<label class="layui-form-label">'.$label.'</label>';
        $formHtml  = '<div class="layui-input-inline">'.$formHtml.$msgHtml.'</div>';
        $html      = '<div class="layui-form-item">'.$labelHtml.$formHtml.'</div>';

        return $html;
    }


}


